package com.qingcheng.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qingcheng.dao.AdminMapper;
import com.qingcheng.dao.AdminRoleMapper;
import com.qingcheng.dao.MemberMapper;
import com.qingcheng.dao.RoleMapper;
import com.qingcheng.entity.PageResult;
import com.qingcheng.pojo.system.Admin;
import com.qingcheng.pojo.system.AdminRole;
import com.qingcheng.service.MemberService;
import com.qingcheng.util.BCrypt;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Map;

@Service
public class MemberServiceImpl implements MemberService {

    @Resource
    private MemberMapper memberMapper;

    @Resource
    private AdminMapper adminMapper;

    @Resource
    private AdminRoleMapper adminRoleMapper;

    @Resource
    private RoleMapper roleMapper;


    @Override
    public PageResult<Map> MemberAll(String loginName, String name, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        Page<Map> pageinfo = (Page<Map>) this.memberMapper.MenberAll(loginName, name);
        return new PageResult<>(pageinfo.getTotal(), pageinfo.getResult());
    }

    @Override
    public int logicDelete(Integer id) {
        return this.memberMapper.logicDelete(id);
    }

    @Override
    @Transactional
    public int insert(Map map) {
        //插入admin表数据
        Admin admin = new Admin();
        admin.setLoginName((String) map.get("login_name"));
        //使用md5加密
        String md5 = (String) map.get("password");
        admin.setPassword(DigestUtils.md5DigestAsHex(md5.getBytes()));
        admin.setCreateTime(LocalDateTime.now());
        admin.setStatus("1");
        admin.setDeleteId("1");
        this.adminMapper.insert(admin);
        //插入admin_role表数据
        AdminRole adminRole=new AdminRole();
        adminRole.setAdminId(admin.getId());
        adminRole.setRoleId((Integer) map.get("role_id"));
        this.adminRoleMapper.insert(adminRole);


        //循环拿到role_id并实行插入
//        List rolelist = new ArrayList();
//        rolelist = (List) map.get("role_id");
//        for (int i = 0; i < rolelist.size(); i++) {
//            AdminRole adminRole = new AdminRole();
//            adminRole.setAdminId(admin.getId());
//            adminRole.setRoleId((Integer) rolelist.get(i));
//            this.adminRoleMapper.insert(adminRole);
//        }
        return 1;
    }

    @Override
    public int updateStatus(String STATUS, Integer id) {
        return this.memberMapper.updateStatus(STATUS, id);
    }

    @Override
    public Map selecybyid(Integer id, Integer role_id,Integer arId) {
        return this.memberMapper.selecybyid(id, role_id,arId);
    }

    @Override
    @Transactional
    public void update(Map map) {
        //修改管理员账户
        Admin admin = new Admin();
        admin.setLoginName((String) map.get("login_name"));
        //md5加密
        String gensalt = BCrypt.gensalt();
        String password = BCrypt.hashpw((String) map.get("password1"), gensalt);

        admin.setPassword(password);
        admin.setId((Integer) map.get("id"));
        this.adminMapper.updateByPrimaryKeySelective(admin);
        //修改管理员的权限
        AdminRole adminRole=new AdminRole();
        adminRole.setRoleId((Integer) map.get("role_id"));
        adminRole.setId((Integer) map.get("arId"));
        this.adminRoleMapper.updateByPrimaryKeySelective(adminRole);
    }

    @Override
    public void deleteRole(Integer id) {
        this.adminRoleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteIdRole(Integer id) {
        this.memberMapper.deleteIdRole(id);
    }

}
